import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'examine-scheme-add',
	templateUrl: './examine-scheme-add.component.html',
})
export class ExamineSchemeAddComponent implements OnInit {
	examineName: string = ''; // 方案名称
  carListArrays: any = []; // 车型多选数组
	selectedCarList: any = [];
	planConfigList: any = [];
	examineType: any = [
		{
			label: '车辆正面',
			value: 1
		},{
			label: '车辆侧面',
			value: 2
		},{
			label: '主机',
			value: 3
		},{
			label: '传感器',
			value: 4
		},{
			label: '天线',
			value: 5
		},{
			label: '其他',
			value: 6
		},{
			label: '电源',
			value: 7
		},{
			label: '行驶证',
			value: 8
		},{
			label: '付费凭证',
			value: 9
		}
	];
	examineTypeId: any = '';
	pageType:string = 'new';
	pageTypeTitle: string = '新增';
	examineMsg: any = '';
	examineNodeName: string = '';
	typeSubscript: number = 0;
	nodeSubscript: number = 0;
	nodeEditStatus: boolean = false;
	examineCarText: any = '';
	editId: any = '';
	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.pageType = queryParams.type;
			if(queryParams.type != 'new') {
				if(queryParams.type == 'edit') {
					this.pageTypeTitle = '编辑'
				}else {
					this.pageTypeTitle = '查看'
				}
				this.editId = queryParams.id;
				this.getExamineMsg(queryParams.id)
			}
		});
	}
	//提交校验
	onSubmit() {
		let carPurpose = [];
		let carClassify = [];
		this.selectedCarList.forEach(ele => {
			if(ele.length == 2){
				carPurpose.push(ele);
			}else {
				carClassify.push(ele);
			}
		});
		
		let data = {
			examineName: this.examineName,
			carPurpose: carPurpose,
			carClassify: carClassify,
			planConfigList: this.planConfigList
		}
		if(this.pageType == 'edit') {
			data = this.examineMsg
			data.examineName = this.examineName
			data.carPurpose = carPurpose
			data.carClassify = carClassify
			data.planConfigList = this.planConfigList
		}
		this.jsonApiService.json_http("examine/examine.html", data).subscribe((result) => {
			if (result.success) {
				this.router.navigate(['/device-management/examine-scheme'],{ queryParams: {nowPage:''}});
			} else {
				this.jsonApiService.message(result.message);
			}
		});
	}
	//返回资源列表页
	goBack() {
		this.router.navigate(['/device-management/examine-scheme']);
	}
	ngOnInit() {
		
	}

	// 获取审核方案
	getExamineMsg(id) {
		this.jsonApiService.http_get(`examine/getExamineById.html?id=${id}`).subscribe((data) => {
      if (data.success) {
				this.examineMsg = data.data;
				this.planConfigList = data.data.planConfigList;
				this.examineName = data.data.examineName;
				let carSelectList = data.data.carPurpose.concat(data.data.carClassify);
				let carListArrays = [];
				carSelectList.forEach(ele =>{
					carListArrays.push(this.recursive(ele, carTypeListObj.carTypeListTree));
				})
				this.carListArrays = carListArrays;
      } else {
        this.jsonApiService.message(data.message);
      }
    });
	}
	recursive(id, arr) {
		for(let ele of arr || []) {
			if(ele.id == id) {
				let obj = {
					id: ele.id,
					text: ele.text
				}
				return obj
			}
			const o_ = this.recursive(id, ele.children)
			if(o_) return o_
		}
	}
  // 车辆用途和车辆分类
  selectedValue(data) {
		console.log(data)
    this.selectedCarList = data.selectedIdList;
		this.examineCarText = data.selectedNameList.join(',');
  }
	// 审核内容select
	examineTypeChange(val) {
		console.log(val)
	}
	// 新增审核内容
	addExamineScheme(modal) {
		this.examineTypeId = '';
		modal.show();
	}
	addExamineMsgClose(modal) {
		modal.hide();
	}
	// 新增审核内容提交
	addExamineMsgSubmit(modal) {
		let repeatState = this.planConfigList.find(item => item.type == this.examineTypeId)
		if(repeatState) {
			this.jsonApiService.message('已存在该类型审核内容');
		}else {
			let selectData = this.examineType.filter(item => item.value == this.examineTypeId)
			let obj = {
				key: selectData[0].label,
				type: selectData[0].value,
				value: []
			}
			this.planConfigList.push(obj)
			modal.hide();
		}
	}
	// 新增审核节点
	addExamineNode(modal, index) {
		this.nodeEditStatus = false;
		this.examineNodeName = '';
		this.typeSubscript = index;
		modal.show();
	}
	// 删除审核内容
	examineDelete(index) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			this.planConfigList.splice(index, 1)
		})
	}
	addExamineNodeClose(modal) {
		modal.hide();
	}
	// 确定添加节点
	addExamineNodeSubmit(modal) {
		this.examineNodeName = this.examineNodeName.replace(/(^\s*)|(\s*$)/g, "")
		if(this.examineNodeName) {
			if(this.nodeEditStatus) {
				this.planConfigList[this.typeSubscript].value[this.nodeSubscript] = this.examineNodeName;
			}else {
				this.planConfigList[this.typeSubscript].value.push(this.examineNodeName);
			}
			modal.hide();
		}else {
			this.jsonApiService.message('请填写判定节点');
		}
	}
	// 编辑判定节点
	examineNodeEdit(row, index, indexNode, modal) {
		this.examineNodeName = row;
		this.typeSubscript = index;
		this.nodeSubscript = indexNode;
		this.nodeEditStatus = true;
		modal.show();
	}
	// 删除判定节点
	examineNodeDelete(index, indexNode) {
		this.planConfigList[index].value.splice(indexNode, 1);
	}
}